layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;

    //动态加载下拉框
    $.ajax({
        type:"GET",
        url:"/admin/project/getLabelAndType",
        success:function (result) {
            var typeList = result.data.labelTypeList.typeList;

            $.each(typeList,function (i,type) {
                var typeName = type.typeName;
                var id = type.id;

                //类型过滤首页
                if(id != 0 ){
                    $("#type").append("<option value='"+id+"'>"+typeName+"</option>");
                }

            })

            //重新渲染下拉框
            form.render("select");
        },
        error:function () {
            layer.msg("系统错误！");
        }
    });

    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 'full-150'
         ,url:"/admin/label/getDataList"
         ,cols: [[
             {field:'labelName',title: '标签名称'}
             ,{field:'keys', title: '关键字'}
             ,{field:'typeName', title: '类型'}
             ,{field:'readCount', title: '阅读量'}
             ,{field:'isShowTitle', title: '是否首页展示',templet:'#showTitle'}
             ,{field:'isTop', title: '是否置顶展示',templet:'#topTool'}
             ,{field:'',title: '操作',toolbar: '#barDemo'}
         ]]
         ,page:true
     });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'edit'){
             //保存到本站
             var url = "/admin/label/toEdit/"+data.labelName;
             $(this).attr("data-url",url);
             parent.addTab($(this));
         }else if(obj.event == 'delete'){
             layer.confirm('确定删除？', function(index){
                 layer.msg('删除中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });
                 $.ajax({
                     type:"POST"
                     ,url:"/admin/label/delete"
                     ,data:{
                         labelName:data.labelName
                     }
                     ,success:function (result) {
                         layer.msg(result.message);
                         table.reload("data");
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         }else {
             //编辑项目
            layer.msg("操作错误！");
         }
     });

    //监听指定开关
    form.on('switch(switchTest)', function(data){
        var isShowTitle = this.checked ? '0':'1';
        var labelName =  data.othis.parents('tr').find("td :first").text();
        $.ajax({
            type:"POST"
            ,url:"/admin/label/updateShowTitile"
            ,data:{
                labelName:labelName,
                isShowTitle:isShowTitle
            }
            ,success:function (res) {
                layer.msg("操作成功");
            }
            ,error:function () {
                layer.msg("服务器错误！");
            }
        });
    });

    //监听指定开关
    form.on('switch(switchTest2)', function(data){
        var isTop = this.checked;
        var labelName =  data.othis.parents('tr').find("td :first").text();
        $.ajax({
            type:"POST"
            ,url:"/admin/label/updateTop"
            ,data:{
                labelName:labelName,
                isTop:isTop
            }
            ,success:function (res) {
                layer.msg("操作成功");
            }
            ,error:function () {
                layer.msg("服务器错误！");
            }
        });
    });

    //行双击查看详情
    table.on('rowDouble(dataTable)',function (obj) {
        var data = obj.data;
        var id = data.id;
        console.log(data);


    });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                labelName:$("#labelName").val(),
                typeId:$("#type").val(),
            }
        })
    });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }



    //跳转到新增页面
    $("#toAdd").on("click",function () {
        var url = "/admin/label/toAdd";
        $(this).attr("data-url",url);
        parent.addTab($(this));
    });

    $("#run").on("click",function () {
        layer.msg('爬取数据中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

        $.ajax({
            type:"POST"
            ,url:"/admin/spider/run"
            ,success:function (res) {
                layer.msg("爬取成功，请查看console");
                 console.log(res.data.result);
                table.reload("data",{
                    page:{
                        curr:1
                    }
                })
            }
            ,error:function () {
                layer.msg("服务器错误！");
            }
        });
    })



});
